import {BRStorage} from "./brStorage.js";

export const BRNative = {
    //切换账号
    changeAccount: () => {
        let runRomSystem = BRStorage.getStorage(BRStorage.KeyRunRomSystem());
        console.log("进入NativeJS-changeAccount系统：" + runRomSystem);
        if (runRomSystem === "android") {
            try {
                window.appAndroidJS.changeAccountJS();
            } catch (err) {
                console.log("Native安卓JS错误changeAccountJS：" + err.message);
            }
        } else {
            let reqData = {
                "handleKey": "changeAccount",
            }
            try {
                window.webkit.messageHandlers.changeAccountJS.postMessage(reqData);
            } catch (err) {
                console.log("Native苹果JS错误changeAccountJS：" + err.message);
            }
        }
    },

    //更新密码
    onPwdChange: (userId, pwd) => {
        let loginName = BRStorage.getStorage(BRStorage.KeyBRLoginName());
        let runRomSystem = BRStorage.getStorage(BRStorage.KeyRunRomSystem());
        console.log("进入NativeJS-onPwdChange系统：" + runRomSystem);
        if (runRomSystem === "android") {
            try {
                window.appAndroidJS.onPwdChangeJS(userId, pwd);
            } catch (err) {
                console.log("Native安卓JS错误onPwdChangeJS：" + err.message);
            }
        } else {
            let reqData = {
                "handleKey": "onPwdChange",
                "userId": userId,
                "pwd": pwd,
            }
            try {
                window.webkit.messageHandlers.onPwdChangeJS.postMessage(reqData);
            } catch (err) {
                console.log("Native苹果JS错误onPwdChangeJS：" + err.message);
            }
        }
    },


    //复制文字信息
    copyText: (contentText) => {
        let runRomSystem = BRStorage.getStorage(BRStorage.KeyRunRomSystem());
        console.log("进入NativeJS-copyText系统：" + runRomSystem);
        if (runRomSystem === "android") {
            try {
                window.appAndroidJS.copyTextJS(contentText);
            } catch (err) {
                console.log("Native安卓JS错误copyTextJS：" + err.message);
            }
        } else {
            let reqData = {
                "handleKey": "copyText",
                "text": contentText,
            }
            try {
                window.webkit.messageHandlers.copyTextJS.postMessage(reqData);
            } catch (err) {
                console.log("Native苹果JS错误copyTextJS：" + err.message);
            }
        }
    },

    //弹出身份证实名认证界面
    bindIdentify: () => {
        let runRomSystem = BRStorage.getStorage(BRStorage.KeyRunRomSystem());
        console.log("进入NativeJS-bindIdentify系统：" + runRomSystem);
        if (runRomSystem === "android") {
            try {
                window.appAndroidJS.bindIdentifyJS();
            } catch (err) {
                console.log("Native安卓JS错误bindIdentifyJS：" + err.message);
            }
        } else {
            let reqData = {
                "handleKey": "bindIdentify",
            }
            try {
                window.webkit.messageHandlers.bindIdentifyJS.postMessage(reqData);
            } catch (err) {
                console.log("Native苹果JS错误bindIdentifyJS：" + err.message);
            }
        }
    },

    //使用系统浏览器打开指定URL
    openUrlByBrowser: (url) => {
        let runRomSystem = BRStorage.getStorage(BRStorage.KeyRunRomSystem());
        console.log("进入NativeJS-openUrlByBrowser系统：" + runRomSystem);
        if (runRomSystem === "android") {
            try {
                window.appAndroidJS.openUrlByBrowserJS(url);
            } catch (err) {
                console.log("Native安卓JS错误openUrlByBrowserJS：" + err.message);
            }
        } else {
            let reqData = {
                "handleKey": "openUrlByBrowser",
                "url": url,
            }
            try {
                window.webkit.messageHandlers.openUrlByBrowserJS.postMessage(reqData);
            } catch (err) {
                console.log("Native苹果JS错误openUrlByBrowserJS：" + err.message);
            }
        }
    },

    //关闭当前WebView
    closeGameWebView: () => {
        let runRomSystem = BRStorage.getStorage(BRStorage.KeyRunRomSystem());
        console.log("进入NativeJS-closeGameWebView系统：" + runRomSystem);
        if (runRomSystem === "android") {
            try {
                window.appAndroidJS.exitWebViewJS();
            } catch (err) {
                console.log("NativeAndroidJS错误exitWebViewJS：" + err.message);
            }
        } else {
            let reqData = {
                "handleKey": "exitWebView"
            }
            try {
                window.webkit.messageHandlers.exitWebViewJS.postMessage(reqData);
            } catch (err) {
                console.log("Native苹果JS错误exitWebViewJS：" + err.message);
            }
        }
    },

    /**
     * 支付完成关闭支付webview且告知研发
     * @param orderNum
     * @param payStatus  int [1=成功,2=取消,3=未知]
     */
    gamePayFinish: (orderNum, payStatus = 1) => {
        let runRomSystem = BRStorage.getStorage(BRStorage.KeyRunRomSystem());
        console.log("进入NativeJS-gamePayFinish支付完成系统：" + runRomSystem);
        if (runRomSystem === "android") {
            try {
                window.appAndroidJS.gamePayFinishJS(orderNum, payStatus);
            } catch (err) {
                console.log("NativeAndroidJS错误gamePayFinishJS：" + err.message);
            }
        } else {
            let reqData = {
                "handleKey": "gamePayFinish",
                "orderNum": orderNum,
                "payStatus": payStatus,
            }
            try {
                window.webkit.messageHandlers.gamePayFinishJS.postMessage(reqData);
            } catch (err) {
                console.log("NativeIOSJS错误gamePayFinishJS：" + err.message);
            }
        }
    },

    /**
     * 关闭
     */
    hideCloseButton: () => {
        let runRomSystem = BRStorage.getStorage(BRStorage.KeyRunRomSystem());
        console.log("进入NativeJS-hideCloseButton系统：" + runRomSystem);
        if (runRomSystem === "android") {
            try {
                window.appAndroidJS.hideCloseButtonJS();
            } catch (err) {
                console.log("NativeAndroidJS错误hideCloseButtonJS：" + err.message);
            }
        } else {
            let reqData = {
                "handleKey": "hideCloseButton",
            }
            try {
                window.webkit.messageHandlers.hideCloseButtonJS.postMessage(reqData);
            } catch (err) {
                console.log("NativeIOSJS错误hideCloseButtonJS：" + err.message);
            }
        }
    },

}


